/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package tfd.scala.squib.demo

import java.awt._
import java.awt.event._
import java.awt.GridBagConstraints._
import javax.swing.{JFrame, JSplitPane, SwingConstants}
import javax.swing.table.{AbstractTableModel}
import javax.swing.event._

import tfd.scala.squib._
import tfd.scala.squib.event._

object AllComponents extends Application {
    
	// The attributeSet of a component type specify attributes that will automatically be 
	// set for any instance of that component afterward.
	//
	// The attributeSets of a class is a mapping regular expression to attributes 
	// that will set of instances whose id mathes the regular expression. It's kind of 
	// cheap CSS-like arrangement.
	// 
	// Ensure all frames exit on close
	// By default make button text blue unless its id is digit then red
	frame.attributesDefault = attributes('defaultcloseoperation->JFrame.EXIT_ON_CLOSE)
	label.attributesDefault = attributes('foreground->Color.orange)
	button.attributesDefault = attributes('foreground->Color.blue)
	button.attributesByPattern += ("\\d" -> attributes('foreground->Color.red))
	
	var flow_panel = panel(
				'layout-> flowlayout('alignment -> FlowLayout.LEFT),
				contents( button("Press", 'text->"Press",'foreground->Color.red),
                     button("Pull", 'text->"Pull",'foreground->Color.blue),
                     canvas(30,30,(g: Graphics) => {
                                    g.setColor(Color.magenta)
                                    g.drawLine(0,0,30,30)
                                    g.drawLine(30,0,0,30)
                     }),
                     checkbox('text->"Check Me"),
                     combobox()
				)
			)
		        
	var border_panel =	panel(
	                'layout->borderlayout(),
                    contents(
                          button("East", 'text->"East") -> "East",
                          splitpane('orientation->JSplitPane.HORIZONTAL_SPLIT,
                                    'leftcomponent -> tree("tree",
                                            attributes('rootVisible->false),
                                            defaultmutabletreenode(
                                                    attributes('userobject -> "Root"),
                                                    contents(
                                                            defaultmutabletreenode(attributes('userobject -> "Node1")),
                                                            defaultmutabletreenode(attributes('userobject -> "Node2"))
                                                    )    
                                            )
                                    ),
                                    'rightcomponent -> list("list",'listData -> Array("one", "two", "three").asInstanceOf[Array[Object]])
                          ) ->"Center",
                          panel("pnl",
								attributes('title->"Test",
								           'layout-> flowlayout('alignment -> FlowLayout.RIGHT)
                                ),
								contents(slider("slider", 'value->100),
										 textfield("value", 'text->"100")
								)
						  ) -> "South"
					)
	)
	
	panel("grid_panel",
                'layout->gridlayout('rows->3, 'columns->3),
                contents(		
                        button("1",'text->"1"),	
						button("2",'text->"2"),
						button("3",'text->"3"),
						button("4",'text->"4"),
						button("5",'text->"5"),
						button("6",'text->"6"),
						button("7",'text->"7"),
						button("8",'text->"8"),
						button("9",'text->"9")	
                )						
	)
	
	panel("calc_panel"
			,'layout-> borderlayout()
			,contents(
				textfield(
						"sum",
						 attributes(
						         'text->"0",
                                 'horizontalalignment->SwingConstants.RIGHT,
                                 'editable->false
				)) -> "North", 
				// Look up by id
				panel.id("grid_panel") -> "Center"
			)
	)
    val gbc = gridbagconstraints('fill -> BOTH, 'weightx -> 1.0, 'weighty-> 0.0)
	var gridbag_panel = panel(
                            'layout->gridbaglayout(),
							contents(
								button('text ->"Button1") -> gbc, 
								button('text ->"Button2") -> gbc,
								button('text ->"Button3") -> gbc,
								button('text ->"Button4") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER), 
								button('text ->"Button5") -> gridbagconstraints(gbc, 'weightx -> 0.0),
								button('text ->"Button6") -> gridbagconstraints(gbc, 'gridwidth -> RELATIVE), 
								button('text ->"Button7") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER), 
								button('text ->"Button8") -> gridbagconstraints(gbc, 'gridheight -> 2, 'weighty -> 1.0),
								button('text ->"Button9") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER),
				                button('text ->"Button10") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER)
							)
	                    )
			
	frame("GUIBuilder Demo",
			'title->"GUIBuilder Demo",
			'jmenubar -> menubar("menubar",
								contents(
									menu("File", 
									    attributes('text-> "File", 'mnemonic->'F'),
										contents(
											menuitem("Open", attributes('text->"Open")),
											menuitem("Close", attributes('text->"Close"))
										 )
									)
								)
							),
			'layout->borderlayout(),
            contents(
				tabbedpane(
					contents (
						flow_panel -> "Flow Layout",
						border_panel-> "Border Layout",
						panel.id("calc_panel") -> "Grid Layout",
						gridbag_panel -> "Grid Bag Layout",
                        scrollpane(
                                table(
                                    attributes('model -> new AbstractTableModel() {
                                                             def getColumnCount() = 10
                                                             def getRowCount() = 10
                                                             def getValueAt(row:Int, col:Int) = "" + (row * col)
                                                         }
                                    )
                                )
                        ) -> "Table"
					)
				) -> "Center",
				splitpane('orientation->JSplitPane.HORIZONTAL_SPLIT,
					      'leftcomponent -> label(attributes('text->"Left")),
						  'rightcomponent -> label(attributes('text->"Right"))
						  
				) -> "South"
			) 
		)
		
	button.events("Press", actionPerformed { (ae: ActionEvent) => 
        button.id("Pull").setForeground(Color.green)
		button.id("Pull").setText("Pulled")
    })
    
	slider.events("slider", 'stateChanged -> { (ce: ChangeEvent) => 
		textfield.id("value").setText(""+ slider.id("slider").getValue())
	})
				
	for (val num<-Iterator.range(1, 10)) {
		button.events("" + num, actionPerformed { (ae: ActionEvent) => 
				var sum = textfield.id("sum")
				sum.setText(sum.getText + num)
		})
	}
    
    menuitem.events("Close", actionPerformed {
        lazy val dlg:Dialog = dialog("dialog",
                'title -> "Confirm Close",
                'modal -> true,
                'layout->borderlayout(),
                contents(
                        label("Are you sure ?") -> "North",
                        panel('layout->flowlayout(),
                              contents(button('text->"Yes", actionPerformed{ System.exit(0) }),
                                       button('text->"No",  actionPerformed{ dlg.hide }))) -> "Center"
    			)
        )
        dlg.pack()
        dlg.show()
    }) 

	var frm = frame.id("GUIBuilder Demo")
	frm.pack
	frm.setVisible(true)
	
} 